package com.amyi.usercenter.controller;

import com.amyi.usercenter.common.BaseResponse;
import com.amyi.usercenter.common.ErrorCode;
import com.amyi.usercenter.common.ResultUtils;
import com.amyi.usercenter.constant.UserConstant;
import com.amyi.usercenter.domain.User;
import com.amyi.usercenter.domain.request.UserLoginRequest;
import com.amyi.usercenter.domain.request.UserRegisterRequest;
import com.amyi.usercenter.exception.BusinessException;
import com.amyi.usercenter.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yyzhang
 * @version 1.0
 */
@RestController
@RequestMapping("/user")
@CrossOrigin(origins = {"http://47.113.228.252"},methods = {RequestMethod.GET,RequestMethod.POST,RequestMethod.OPTIONS}
,allowCredentials = "true")
public class UserRegister {
    @Resource
    private UserService userService;
    @RequestMapping("/register")
    //@RequestBody使前端传来的对象和这个对象做关联
    public BaseResponse<Long> register(@RequestBody UserRegisterRequest userRegisterRequest){
        if (userRegisterRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        String planetCode = userRegisterRequest.getPlanetCode();
        //controller层是对数据本身校验，不涉及业务逻辑，重复一次保险
        if(StringUtils.isAnyBlank(userAccount,userPassword,checkPassword)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数不能为空");
        }
        long result = userService.userRegister(userAccount, userPassword, checkPassword, planetCode);
        return ResultUtils.success(result);


    }

    @RequestMapping("/login")
    //@RequestBody使前端传来的对象和这个对象做关联
    public BaseResponse<User> login(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest httpServletRequest){
        if (userLoginRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        //controller层是对数据本身校验，不涉及业务逻辑，重复一次保险
        if(StringUtils.isAnyBlank(userAccount,userPassword)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数不能为空");
        }
        User user = userService.login(userAccount, userPassword, httpServletRequest);
        return ResultUtils.success(user);

    }

    @RequestMapping("/logout")
    //@RequestBody使前端传来的对象和这个对象做关联
    public BaseResponse<Integer> logout( HttpServletRequest httpServletRequest){
        if (httpServletRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        int result = userService.userLogout(httpServletRequest);
        return ResultUtils.success(result);
    }

    @GetMapping("/search")
    public BaseResponse<List<User>> searchUsers(String username,HttpServletRequest request) {
        List<User> result = userService.searchUsers(username,request);

        return ResultUtils.success(result);
    }
    @GetMapping("/current")
    public BaseResponse<User> currentUser(HttpServletRequest request){
        User user=userService.getCurrentUser(request);
        return ResultUtils.success(user);
    }
        @PostMapping("/delete")
        public BaseResponse<Boolean> deleteUser(@RequestBody long id,HttpServletRequest request) {
        boolean result=userService.deleteUser(id,request);
            return ResultUtils.success(result);
        }
}
